package com.mjk.manager.magicapi.core.core.config;

import com.mjk.common.util.ObjectUtils;
import com.mjk.manager.magicapi.core.core.servlet.MagicHttpServletRequest;

import com.mjk.manager.magicapi.core.core.servlet.MagicHttpServletResponse;
import org.springframework.http.HttpHeaders;

public abstract class MagicCorsFilter {

    public void process(MagicHttpServletRequest request, MagicHttpServletResponse response) {
        if (ObjectUtils.isNotEmpty(Constants.HEADER_REQUEST_CLIENT_ID)) {
            String value = request.getHeader(HttpHeaders.ORIGIN);
            response.setHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, ObjectUtils.isEmpty(value) ? "*" : value);
            response.setHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS, Constants.CONST_STRING_TRUE);
            value = request.getHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS);
            if (ObjectUtils.isNotEmpty((value))) {
                response.setHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, value);
            }
            value = request.getHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD);
            response.setHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, ObjectUtils.isEmpty(value) ? "GET,POST,OPTIONS,PUT,DELETE" : value);
        }
    }
}
